package com.tedu.dao;

import com.tedu.demo1.User;
import com.tedu.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Author：毕龙
 * Date: 2024/6/14 15:37
 */
public class UserDaoImpl implements UserDao{
    @Override
    public List<User> listUsers() throws SQLException {
        //获取链接
        Connection conn = JDBCUtil.getConnection();

        //获取PreparedStatement
        String sql = "select id, username, password, birthday from user";
        PreparedStatement pstm = conn.prepareStatement(sql);

        //执行sql
        ResultSet rs = pstm.executeQuery();
        //创建一个集合
        ArrayList<User> list = new ArrayList<>();
        //处理查询结果
        while (rs.next()){
            int id = rs.getInt(1);
            String name = rs.getString(2);
            String pass = rs.getString(3);
            java.sql.Date date = rs.getDate(4);
            list.add(new User(id,name ,pass ,date));
        }
        //释放资源
        JDBCUtil.release(rs,pstm,conn);

        return list;
    }

    @Override
    public User getUserById(Integer id) {
        return null;
    }

    @Override
    public int insertUser(User user) {
        return 0;
    }
}
